<template>
  <a-card title="领取概况" :style="{ marginTop: '20px' }">
    <div class="table-page-search-wrapper">
      <a-form-model :model="queryParam" layout="inline">
        <a-row :gutter="20">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="顾问姓名">
              <a-select v-model="queryParam['filter[username]']" placeholder="选择顾问姓名">
                <a-select-option v-for="(item,index) in adviserOpts" :value="index" :key="index">{{item}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="联系电话">
              <a-input type="text" v-model="queryParam['filter[phone]']" />
            </a-form-model-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="领取时间">
                <a-range-picker @change="onChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="资源类型">
                <a-select v-model="queryParam['filter[type]']" placeholder="选择资源类型">
                  <a-select-option v-for="(item,index) in typeOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operator">
      <s-table
        bordered
        ref="table"
        size="middle"
        :row-key="(record) => record.customer_id"
        :columns="totalColumns"
        :data="totalData"
      >
<!--        <div slot="title">
          <a-radio-group default-value="1" button-style="solid" @change="onChange1">
            <a-radio-button value="1">
              全部
            </a-radio-button>
            <a-radio-button value="2">
              未添加微信
            </a-radio-button>
          </a-radio-group>
        </div>-->
        <div
          slot="filterDropdown"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-input
            v-ant-ref="c => (searchInput = c)"
            :placeholder="`筛选${column.title}`"
            :value="selectedKeys[0]"
            style="width: 188px; margin-bottom: 8px; display: block;"
            @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
          />
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
          >
            筛选
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
            重置
          </a-button>
        </div>
        <template slot="type" slot-scope="text">
          <a-tag color="red">{{ typeOpts[text] }}</a-tag>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button type="primary" size="small" @click="$router.push({ name: 'OtherDetail', params: {id: record.customer_id}})">详情</a-button>
        </template>
      </s-table>
    </div>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getOtherCustomer } from '@/api/customer'
import customerColumns from '@/columns/customer'

export default {
  components: {
    STable
  },
  name: 'OtherCustomer',
  data () {
    return {
      totalColumns: customerColumns.other2,
      totalData: parameters => {
        return getOtherCustomer(Object.assign(parameters, this.queryParam)).then(res => {
          this.adviserOpts = res.result.adviser_array
          this.typeOpts = res.result.type_opts
          return res.result
        })
      },
      searchInput: null,
      searchText: '',
      queryParam: {},
      advanced: true,
      adviserOpts: {},
      typeOpts: {}
    }
  },
  methods: {
    handleSearch (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
    },
    handleReset (clearFilters) {
      clearFilters()
      this.searchText = ''
    },
    onChange (date, dateString) {
      this.queryParam['filter[begin_received_at]'] = dateString[0]
      this.queryParam['filter[end_received_at]'] = dateString[1]
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onChange1 (e) {
      if (e.target.value === '1') {
        this.queryParam['filter[wechat]'] = ''
        this.$refs.table.refresh(true)
      } else {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style scoped>

</style>
